import React, { Component } from "react";
import { Table, Button } from "antd";
import LinkButton from "../../components/link-button/index";

class Whitelists extends Component {
  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      selectedRowKeys: [],
      columns: [
        {
          title: "文件名",
          dataIndex: "name",
          render: (text) => <LinkButton>{text}</LinkButton>,
        },
        {
          title: "度量值",
          dataIndex: "age",
        },
        {
          title: "软件类型",
          dataIndex: "address",
        },
        {
          title: "操作",
          render: (text, record, index) => (
            <div>
              <LinkButton>详情</LinkButton>&nbsp;&nbsp;
              <LinkButton
                type="danger"
                size={"small"}
                onClick={() => this.delete1()}
              >
                删除
              </LinkButton>
            </div>
          ),
        },
      ],
      data: [
        {
          key: "1",
          name: "deploy. _tee exe",
          age: "C3A7F9C86E717E1E284709E3823196978A0D8F36930B5CCAD2F960DB11A427DE",
          address: "deplioy_ftee exe",
        },
        {
          key: "2",
          name: "3602ip exe",
          age: "7734FD66675ADC617337E952A70EBEE887EB74BDL497EA0F2E90A5U28D1C8FOC",
          address: "3602ip exe",
        },
        {
          key: "3",
          name: "ligcn.exe",
          age: "62F006178C2F577A1E32ADEE8B50F8E17091EA45842F13F476BE968D3C994C3F",
          address: "ligcn.exe",
        },
        {
          key: "4",
          name: "Rar.exe",
          age: "01576609B4657B0A409FA420LB3AACUFA56220465907CC6A3A1DA54661A65A4F",
          address: "Rar.exe",
        },
      ],
    };
  }
  delete1 = () => {
    const selectedRowKeys = this.state.selectedRowKeys;
    let dataSource = this.state.data;
    for (let i = selectedRowKeys; i >= 0; i--) {
      dataSource.splice(selectedRowKeys, 1);
    }
    // for (let i = selectedRowKeys.length - 1; i >= 0; i--) {
    //   dataSource.splice(Number.parseInt(selectedRowKeys[i] - 1), 1);
    // }
    this.setState({
      data: dataSource,
      selectedRowKeys: [],
    });
  };
  delete = () => {
    const selectedRowKeys = this.state.selectedRowKeys;
    let dataSource = this.state.data;
    // for (let i = selectedRowKeys; i >= 0; i--) {
    //   dataSource.splice(selectedRowKeys, 1);
    // }
    for (let i = selectedRowKeys.length - 1; i >= 0; i--) {
      dataSource.splice(Number.parseInt(selectedRowKeys[i] - 1), 1);
    }
    this.setState({
      data: dataSource,
      selectedRowKeys: [],
    });
  };
  onSelectChange = (selectedRowKeys) => {
    this.setState({ selectedRowKeys });
  };
  render() {
    const { selectedRowKeys, data, columns, loading } = this.state;

    const rowSelection = {
      type: "checkbox",
      selectedRowKeys: selectedRowKeys,
      onChange: this.onSelectChange.bind(this),
    };
    const hasSelected = selectedRowKeys.length > 0;
    return (
      <div style={{ float: "left", width: "80%" }}>
        <div>
          <Button
            type="danger"
            onClick={this.delete.bind(this)}
            disabled={!hasSelected}
            loading={loading}
          >
            删除
          </Button>
        </div>
        <Table
          rowSelection={rowSelection}
          columns={columns}
          dataSource={data}
        />
      </div>
    );
  }
}

export default Whitelists;
